#include "q_data.h"
#include "q_modify.h"
#include "q_seek.h"
#include "system.h"
#include "ui_q_data.h"
#include <QDebug>
#include <QSqlQuery>

#include <QMenu>
#include <QAction>

Q_data::Q_data(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::Q_data)
{
    ui->setupUi(this);

    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    table_widget_menu = new QMenu(ui->tableWidget);
    QAction* action = new QAction("修改", this);
    connect(action, SIGNAL(triggered()), this, SLOT(slotActionModify()));
    table_widget_menu->addAction(action);
    action = new QAction("删除", this);
    connect(action, SIGNAL(triggered()), this, SLOT(slotActionRemove()));
    table_widget_menu->addAction(action);
    action = new QAction("查找", this);
    connect(action, SIGNAL(triggered()), this, SLOT(slotActionSeek()));
    table_widget_menu->addAction(action);

    qDebug() << QSqlDatabase::drivers();
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    m_db.setHostName("localhost");//主机名
    m_db.setPort(3306);
    m_db.setDatabaseName("user");
    m_db.setUserName("root");
    m_db.setPassword("123456");
    m_db.open();
    if(m_db.open())
    {
        qDebug() << "连接成功";
        showStuInfo();
    }
    else
    {
        qDebug() << "连接失败！！";
    }
}



Q_data::~Q_data()
{
    delete ui;
}

void Q_data::showStuInfo()
{
    QSqlQuery query;
    query.exec("SELECT * FROM wenjuan");
    while(query.next())
    {
        QString name = query.value(0).toString();
        QString age = query.value(1).toString();
        QString banji = query.value(2).toString();
        QString hoppy = query.value(3).toString();
        qDebug() << QString("name %1 age %2 banji 3% hoppy 4% ").arg(name).arg(age).arg(banji).arg(hoppy);

        QTableWidgetItem* nameItem = new QTableWidgetItem(name);
        QTableWidgetItem* ageItem = new QTableWidgetItem(age);
        QTableWidgetItem* banjiItem = new QTableWidgetItem(banji);
        QTableWidgetItem* hoppyItem = new QTableWidgetItem(hoppy);


        int row = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row, 0, nameItem);
        ui->tableWidget->setItem(row, 1, ageItem);
        ui->tableWidget->setItem(row, 2, banjiItem);
        ui->tableWidget->setItem(row, 3, hoppyItem);
    }
}
void Q_data::slotActionSeek()
{
    Q_seek* m = new Q_seek();
    m->show();
    this->close();

}
//返回
void Q_data::on_pushButton_clicked()
{
    System* m = new System();
    m->show();
    this->close();
}
//修改
void Q_data::slotActionModify()
{
    Q_modify* m = new Q_modify();
    m->show();
    this->close();
}
//右键菜单删除
void Q_data::slotActionRemove()
{

    QList<QTableWidgetItem*> selectedItem = ui->tableWidget->selectedItems();
    if(selectedItem.isEmpty())
    {
        qDebug() << "未选中内容！！！";
        return;
    }

    int row = ui->tableWidget->currentRow();
    qDebug() << "on modifybtn clicked" << row;
    QString name = ui ->tableWidget ->item(row, 0)->text();
    QString age = ui ->tableWidget ->item(row, 1)->text();
    QString banji = ui ->tableWidget ->item(row, 2)->text();
    QString hoppy = ui ->tableWidget ->item(row, 3)->text();


    QSqlQuery query;

    query.prepare("DELETE FROM wenjuan WHERE name=? AND age=? AND banji=? AND hoppy=?");
    query.addBindValue(name);
    query.addBindValue(age);
    query.addBindValue(banji);
    query.addBindValue(hoppy);
    if(query.exec())
    {
        qDebug() << "更新成功！";
        ui->tableWidget->removeRow(row);
    }
    else
    {
        qDebug() << "更新失败！";
    }

}

//右键菜单
void Q_data::on_tableWidget_customContextMenuRequested(const QPoint& pos)
{
    table_widget_menu->exec(QCursor::pos());
}




////查找按钮
//void Q_data::on_pushButtonSeek_clicked()
//{

//    QString info = ui->lineEditSeekName->text();
//    QSqlQuery query;
//    query.prepare("SELECT * FROM wenjuan WHERE name or age  LIKE ? ");
//    query.addBindValue("%" + info + "%");
//    if(query.exec())
//    {
//        qDebug() << "更新成功！";
//        ui->tableWidget_2->clearContents();
//        ui->tableWidget_2->setRowCount(0);
//        while(query.next())
//        {
//            QString name = query.value(0).toString();
//            QString age = query.value(1).toString();
//            QString banji = query.value(2).toString();
//            QString hoppy = query.value(3).toString();
//            qDebug() << QString("name %1 age %2 banji 3% hoppy 4% ").arg(name).arg(age).arg(banji).arg(hoppy);

//            QTableWidgetItem* nameItem = new QTableWidgetItem(name);
//            QTableWidgetItem* ageItem = new QTableWidgetItem(age);
//            QTableWidgetItem* banjiItem = new QTableWidgetItem(banji);
//            QTableWidgetItem* hoppyItem = new QTableWidgetItem(hoppy);


//            int row = ui->tableWidget_2->rowCount();
//            ui->tableWidget_2->insertRow(row);
//            ui->tableWidget_2->setItem(row, 0, nameItem);
//            ui->tableWidget_2->setItem(row, 1, ageItem);
//            ui->tableWidget_2->setItem(row, 2, banjiItem);
//            ui->tableWidget_2->setItem(row, 3, hoppyItem);
//        }
//    }
//    else
//    {
//        qDebug() << "更新失败！";
//    }

//}
//删除按钮
void Q_data::on_pushButtonDel_clicked()
{

    qDebug() << "删除按钮被点击了！";
    QList<QTableWidgetItem*> selectedItem = ui->tableWidget->selectedItems();
    if(selectedItem.isEmpty())
    {
        qDebug() << "未选中内容！！！";
        return;
    }

    int row = ui->tableWidget->currentRow();
    qDebug() << "on modifybtn clicked" << row;
    QString name = ui ->tableWidget ->item(row, 0)->text();
    QString age = ui ->tableWidget ->item(row, 1)->text();
    QString banji = ui ->tableWidget ->item(row, 2)->text();
    QString hoppy = ui ->tableWidget ->item(row, 3)->text();


    QSqlQuery query;

    query.prepare("DELETE FROM wenjuan WHERE name=? AND age=? AND banji=? AND hoppy=?");
    query.addBindValue(name);
    query.addBindValue(age);
    query.addBindValue(banji);
    query.addBindValue(hoppy);
    if(query.exec())
    {
        qDebug() << "更新成功！";
        ui->tableWidget->removeRow(row);
    }
    else
    {
        qDebug() << "更新失败！";
    }


}
